<?php
// +----------------------------------------------------------------------
// | Site:  [ http://www.yzmcms.com]
// +----------------------------------------------------------------------
// | Copyright: 袁志蒙工作室，并保留所有权利
// +----------------------------------------------------------------------
// | Author: YuanZhiMeng <214243830@qq.com>
// +---------------------------------------------------------------------- 
// | Explain: 这不是一个自由软件,您只能在不用于商业目的的前提下对程序代码进行修改和使用，不允许对程序代码以任何形式任何目的的再发布！
// +----------------------------------------------------------------------

defined('IN_YZMPHP') or exit('Access Denied'); 
yzm_base::load_controller('common', 'admin', 0);
yzm_base::load_common('lib/content_form'.EXT, 'admin');
yzm_base::load_common('function/hr.func.php');
yzm_base::load_sys_class('page','',0);

class hr_leave extends common {

	/**
	 * 岗位列表
	 */
	public function leave_list() {
		$of = input('get.of');
		$or = input('get.or');
		$of = in_array($of, array('l.id','m.department_id','l.add_time','l.action_time','l.add_time')) ? $of : 'l.id';
		$or = in_array($or, array('ASC','DESC')) ? $or : 'DESC';
		$get = $_GET;
		$where = '1=1';
		$dosubmit = $get['dosubmit'] ?? '';
		$name = $get['name']??'';
		$department_id = $get['department_id']??'';
		$is_ss = $get['is_ss']??'';
		if($dosubmit == 1){
			if(trim($name)!=''){
				$where .=' AND m.name LIKE "%'.$get['name'].'%"';
			}
			if($department_id!=''){
				$list = D('hr_department')->field('id')->where('parentid='.$get['department_id'])->select();
				//print_r($list);exit;
				if(!$list){
					$where .= ' AND m.department_id = '.$get['department_id'];
				}else{
					$ids = array_column($list, 'id');
					$where .= ' AND m.department_id in ('.implode(',',$ids).')';	
				}	
			}
		}
		
		$tree = yzm_base::load_sys_class('tree');
		$department_list = D('hr_department')->order('listorder ASC,id ASC')->limit(1000)->select();
		$array = array();
		foreach($department_list as $v) {
			$v['selected'] = $v['id'] == $department_id ? 'selected' : '';
			$array[] = $v;
		}
		$str  = "<option value='\$id' \$selected>\$spacer \$name</option>";
		$tree->init($array);
		$select_menus = $tree->get_tree(0, $str);
		
		$total = D('hr_leave_list')->alias('l')->join('yzm_hr_member m ON m.id = l.member_id','left')->where($where)->total();
		//echo D('hr_money_list')->lastsql();exit;
		$page = new page($total, 30);
		$data = D('hr_leave_list')->alias('l')->field('l.*,m.name,m.department_id,m.position_id')->join('yzm_hr_member m ON m.id = l.member_id','left')->where($where)->order("$of $or")->limit($page->limit())->select();
		/*
		foreach($data as &$v){
			$v['department_name'] = department($v['department_id']);
		}*/
		include $this->admin_tpl('hr/leave_list');
	}
	
	
	/**
	 * 添加岗位
	 */
	public function leave_add() {
		if(isset($_POST['dosubmit'])){
			$post =$_POST;
			if(!trim($post['member_id'])) return_json(array('status'=>0,'message'=>'请选择员工'));				
			$data = [
				'member_id'=>$post['member_id'],
				'days'=>trim($post['days']),
				'add_time'=>time(),
				'action_time'=>strtotime($post['action_time']),
				'remark'=>trim($post['remark'])
			];
			$member = D('hr_member')->where(['id'=>$post['member_id']])->find();
			
			//新增是判断
			if($post['days']>$member['leave_days']&&!$post['id']){
				return_json(array('status'=>0,'message'=>'休息天数超过年假剩余天数！'));
			}
			
			$position = position($member['position_id']);
			if($post['days']>$position['leave_days']){
				return_json(array('status'=>0,'message'=>'休息天数超过该岗位年假天数！'));
			}
			
			if($post['id']){
				$result = D('hr_leave_list')->update($data,['id'=>$post['id']]);
				//echo D('hr_leave_list')->lastsql();exit;
			}else{
				$result = D('hr_leave_list')->insert($data,true);
			}
			if($result){
				$result = calc_member_leave($member);
				if($result){
					return_json(array('status'=>1,'message'=>L('operation_success')));
				}else{
					return_json(array('status'=>0,'message'=>'计算年假失败！'));
				}
			}else{
				return_json(array('status'=>0,'message'=>L('operation_failure')));
			}
		}
		if(isset($_GET['id'])){
			//编辑员工，获取员工资料
			$where = 'l.id = '.$_GET['id'];
			$data = D('hr_leave_list')->alias('l')->field('m.id as member_id,m.name,m.position_id,m.department_id,l.*')->join('yzm_hr_member m ON m.id = l.member_id','left')->where($where)->find();
		}elseif(isset($_GET['member_id'])){
			$where = 'm.id = '.$_GET['member_id'];
			$data = D('hr_member')->alias('m')->field('id as member_id,name,position_id,department_id')->where($where)->find();
			$data['days'] = '';
			$data['id'] = '';
			$data['remark'] = '';
			$data['action_time'] = '';
		}else{
			//添加员工，默认数据
			$data = [
				'name'=>'',
				'positin_id'=>'',
				'department_id'=>'',
				'days'=>'',
				'id'=>'',
				'action_time'=>'',
				'member_id'=>'',
				'remark'=>'',
			];
			if(isset($_GET['member_id'])){
				$member = D('hr_member')->where(['id'=>$_GET['member_id']])->find();
				$data['name'] = $member['name'];
				$data['member_id'] = $member['id'];
				$data['position_id'] = $member['position_id'];
				$data['department_id'] = $member['department_id'];
			}
		}
		$member_list = D('hr_member')->where(['status'=>1])->order('department_id asc,id asc')->select();
		include $this->admin_tpl('hr/leave_add');
	}
	
	/**
	 * 删除用户
	 */	
	public function leave_delete(){
		if($_POST && is_array($_POST['ids'])){
			$leave_list = D('hr_leave_list');
			foreach($_POST['ids'] as $val){
				$leave = $leave_list->where(['id'=>$val])->find();
				$result = calc_member_leave($leave['member_id']);
				if($result['status'] == 1){
					$leave_list->delete(array('id'=>$val));
				}else{
					return_json(array('status'=>0,'message'=>'记录ID:'.$val.'对应员工重新计算年假失败！'));
				}
			}
			showmsg(L('operation_success'),'',1);
		}
	}
	
	/**
	 * 编辑岗位
	 */
	public function edit() {				
		$db = D('hr_position');
		if(isset($_POST['dosubmit'])) {
			$id = isset($_POST['id']) ? intval($_POST['id']) : 0;
			if($db->update($_POST, array('id' => $id), true)){
				return_json(array('status'=>1,'message'=>L('operation_success')));
			}else{
				return_json();
			}
			
		}else{
			$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
			$r = $db->where(array('id' => $id))->find();
			if($r) extract($r);

			$tree = yzm_base::load_sys_class('tree');
			$department_list = D('hr_department')->where(['display'=>1])->order('listorder ASC,id ASC')->limit(1000)->select();
			$array = array();
			foreach($department_list as $v) {
				$v['selected'] = $v['id'] == $r['department_id'] ? 'selected' : '';
				$array[] = $v;
			}
			$str  = "<option value='\$id' \$selected>\$spacer \$name</option>";
			$tree->init($array);
			$select_menus = $tree->get_tree(0, $str);

			include $this->admin_tpl('hr/position_edit');
		}
	}
	
	public function text(){
		$id = $_GET['id'];
		if(!$id){
			exit('参数错误！');
		}
		$info = D('hr_position')->where(array('id' => $id))->find();
		include $this->admin_tpl('hr/position_text');
	}


	/**
	 * 岗位排序
	 */
	public function order() {
		if(isset($_POST['dosubmit'])) {
			$db = D('hr_department');
			foreach($_POST['listorders'] as $id => $listorder) {
				$db->update(array('listorder'=>$listorder),array('id'=>$id));
			}
			showmsg(L('operation_success'), '', 1);
		} else {
			showmsg(L('operation_failure'));
		}
	}	


	/**
	 * 岗位隐藏显示
	 */
	public function public_change_status() {
		if(is_post()){
			$id = isset($_POST['id']) ? intval($_POST['id']) : 0;
			$value = isset($_POST['value']) ? intval($_POST['value']) : 0;
			if(D('hr_position')->update(array('status'=>$value), array('id' => $id))){
				return_json(array('status'=>1,'message'=>L('operation_success')));
			}else{
				return_json();
			}
		}
	}
	
}